<?php
namespace App\Common\Lib\Err;

use App\Common as com;

class Code extends com\Lib\LibBase {
	/** 0：未定义的错误 */
	public static int $undefined = 0;
	/** -100000：错误码上限 */
	public static int $errorLimitNum = -100000;

	//==========================
	//=== 框架错误(通用)
	//==========================
	/** -100001：错误(需要用文字说明) */
	public static int $sysData = -100001;
	/** -100002：配置不存在 */
	public static int $sysConfigNotExist = -100002;
	/** -100003：断言错误 */
	public static int $sysAssertError = -100003;
	/** -100004：数据类型错误 */
	public static int $sysDataTypeError = -100004;
	/** -100005：不支持的类型 */
	public static int $sysNonsupportType = -100005;

	//==========================
	//=== 框架错误->其他
	//==========================
	/** -101001：异步操作 */
	public static int $sysOtherAtm = -101001;
	/** -101002：服务器环境检测不通过 */
	public static int $sysOtherServerEnvironmentNoPass = -101002;
	/** -101003：服务器必须支持事务嵌套 */
	public static int $sysOtherMustSupportSavepoint = -101003;
	/** -101004：没有权限 */
	public static int $sysLogRecordLose = -101004;
	/** -101005：第三方调用错误 */
	public static int $sysExternalError = -101005;

	//==========================
	//=== 框架错误->函数
	//==========================
	/** -102001：函数的参数错误 */
	public static int $sysFunParamError = -102001;
	/** -102002：函数调用发生异常错误 */
	public static int $sysFunExecuteError = -102002;


	//==========================
	//=== 框架错误（数据类）
	//==========================
	/** -103001：数据错误 */
	public static int $sysDataError = -103001;
	/** -103002：数据不存在 */
	public static int $sysDataNotExist = -103002;
	/** -103003：数据已存在 */
	public static int $sysDataExist = -103003;
	/** -103004：数据验证错误 */
	public static int $sysDataValidateError = -103004;
	/** -103005：没有权限修改数据 */
	public static int $sysDataNotAuthSavaData = -103005;
	/** -103006：数据丢失 */
	public static int $sysDataLose = -103006;
	/** -103007：数据过期 */
	public static int $sysDataExpire = -103007;
	/** -103008：未知数据 */
	public static int $sysDataUnknown = -103008;

	//==========================
	//=== 框架错误->数据库操作相关
	//==========================
	/** -104001：SQL执行错误 */
	public static int $sysSqlExecError = -104001;
	/** -104002：数据错误 */
	public static int $sysSqlDataError = -104002;
	/** -104003：数据已存在 */
	public static int $sysSqlDataExist = -104003;
	/** -104004：数据不存在 */
	public static int $sysSqlDataNotExist = -104004;
	/** -104005：数据保存失败 */
	public static int $sysSqlSavaDataError = -104005;
	/** -104006：数据插入失败 */
	public static int $sysSqlInsertError = -104006;
	/** -104007：数据更新失败 */
	public static int $sysSqlUpdateError = -104007;
	/** -104008：数据删除失败 */
	public static int $sysSqlDeleteError = -104008;
	/** -104009：事务保存点错误 */
	public static int $sysSqlSavepointError = -104009;
	/** -104010：事务作用域已存在 */
	public static int $sysSqlTransScopeExist = -104010;
	/** -104011：事务作用域不存在 */
	public static int $sysSqlTransScopeNoExist = -104011;
	/** -104012：无权操作该事务作用域 */
	public static int $sysSqlTransScopeNoAuth = -104012;
	/** -104013：退出作用域失败 */
	public static int $sysSqlTransScopeQuitLose = -104013;
	/** -104014：上个事务未结束 */
	public static int $sysSqlTransNotFinish = -104014;
	/** -104015：事务回滚失败 */
	public static int $sysSqlTransRollbackLose = -104015;


	//==========================
	//=== 框架错误->文件
	//==========================
	/** -105001：文件不存在 */
	public static int $sysFileNotExist = -105001;


	//==========================
	//=== 框架错误（锁）
	//==========================
	/** -106001：没有权限 */
	public static int $sysNotAuth = -106001;
	/** -106002：上锁失败 */
	public static int $sysLockLose = -106002;

	//==========================
	//===APP通用->通用错误
	//==========================
	/** -201001：事件执行失败 */
	public static int $appComEventLose = -201001;


	//==========================
	//=== API错误(系统级错误代码)
	//==========================

	/** -202001：系统错误 */
	public static int $apiSysError = -202001;
	/** -202002：API不存在 */
	public static int $apiSysApiNoExist = -202002;
	/** -202003：api调用错误 */
	public static int $apiSysExecuteError = -202003;
	/** -202004：参数错误，请参考API文档 */
	public static int $apiSysParamError = -202004;

	//==========================
	//===API错误(服务级错误代码)
	//==========================

	/** -203001：app_id不存在 */
	public static int $apiSerOAuthAppIdError = -203001;
	/** -203002：app_secret错误 */
	public static int $apiSerOAuthAppSecretError = -203002;
	/** -203003：认证不通过 */
	public static int $apiSerOAuthAuthNoPass = -203003;
	/** -203004：认证不合法 */
	public static int $apiSerTokenIllegal = -203004;
	/** -203005：Token已经过期 */
	public static int $apiSerTokenOverdue = -203005;
	/** -203006：Token不存在 */
	public static int $apiSerTokenNoExist = -203006;
	/** -203007：签名错误 */
	public static int $apiSerSignError = -203007;
	/** -203008：没有使用权限 */
	public static int $apiSerNoUseAuth = -203008;
	/** -203009：时间戳过期 */
	public static int $apiSerTimestamp = -203009;

	//==========================
	//===API错误(业务级错误代码)
	//==========================

	/** -204001：设备不在线 */
	public static int $apiBusinessFacilityModuleOffline = -204001;

	//==========================
	//===业务错误代码
	//==========================

	/** -300101：设备->售后->故障订单->售后订单状态无法修改 */
	public static int $appFacilityAftermarketMalfunctionOrder_StatusChangeError = -300101;
	/** -300102：设备->售后->故障订单->售后订单状态不可修改为此状态 */
	public static int $appFacilityAftermarketMalfunctionOrder_StatusChangeNotMatch = -300102;

	public static array $code = [
		// 0 => "",
		-1000 => "错误码上限",

		//==========================",
		//=== 框架错误(通用)",
		//==========================",
		-100001 => "错误",
		-100002 => "配置不存在",
		-100003 => "断言错误",
		-100004 => "数据类型错误",
		-100005 => "不支持的类型",
		//==========================",
		//=== 框架错误->其他",
		//==========================",
		-101001 => "异步操作",
		-101002 => "服务器环境检测不通过",
		-101003 => "服务器必须支持事务嵌套",
		-101004 => "没有权限",
		-101005 => "第三方调用错误",

		//==========================",
		//=== 框架错误",
		//==========================",
		-102001 => "函数的参数错误",
		-102002 => "函数调用发生异常错误",

		//==========================",
		//=== 框架错误（数据类）",
		//==========================",
		-103001 => "数据错误",
		-103002 => "数据不存在",
		-103003 => "数据已存在",
		-103004 => "数据验证错误",
		-103005 => "没有权限修改数据",
		-103006 => "数据丢失",
		-103007 => "数据过期",
		-103008 => "未知数据",

		//==========================",
		//=== 框架错误->数据库操作相关",
		//==========================",
		-104001 => "SQL执行错误",
		-104002 => "数据错误",
		-104003 => "数据已存在",
		-104004 => "数据不存在",
		-104005 => "数据保存失败",
		-104006 => "数据插入失败",
		-104007 => "数据更新失败",
		-104008 => "数据删除失败",
		-104009 => "事务保存点错误",
		-104010 => "事务作用域已存在",
		-104011 => "事务作用域不存在",
		-104012 => "无权操作该事务作用域",
		-104013 => "退出作用域失败",
		-104014 => "上个事务未结束",
		-104015 => "事务回滚失败",

		//==========================",
		//=== 框架错误->文件",
		//==========================",
		-105001 => "文件不存在",

		//==========================",
		//=== 框架错误（锁）",
		//==========================",
		-106001 => "没有权限",
		-106002 => "上锁失败",

		//==========================",
		//===APP通用->通用错误",
		//==========================",
		-201001 => "事件执行失败",

		//==========================",
		//=== API错误(系统级错误代码)",
		//==========================",
		-202001 => "系统错误",
		-202002 => "API不存在",
		-202003 => "api调用错误",
		-202004 => "参数错误，请参考API文档",

		//==========================",
		//===API错误(服务级错误代码)",
		//==========================",
		-203001 => "app_id不存在",
		-203002 => "app_secret错误",
		-203003 => "认证不通过",
		-203004 => "认证不合法",
		-203005 => "Token已经过期",
		-203006 => "Token不存在",
		-203007 => "签名错误",
		-203008 => "没有使用权限",
		-203009 => "时间戳过期",

		//==========================",
		//===API错误(业务级错误代码)",
		//==========================",
		-204001 => "设备不在线",

		//==========================",
		//===业务错误代码",
		//==========================",
		-300101 => "售后订单状态无法修改",
		-300102 => "售后订单状态不可修改为此状态",

	];

	/**
	 * 获取错误码说明
	 * @param int $error_code 错误码
	 * @param array $param 参数数组 <br />
	 * ················· [可空](str)	$param['default_str']	说明：模式的错误说明。['']默认	<br />
	 * ················· [可空/多选_A/必须](unknown_type)	$param['AAAAA']	说明：AAAAA	<br />
	 * @return string 错误说明
	 */
	public static function getText( $error_code, array $param = [] ) {
		// ...
		$param['default_str'] = $param['default_str'] ?? '';
		//
		return self::$code[ $error_code ] ?? $param['default_str'];
	}

	/**
	 * 获取错误码说明(优先获取针对用户的错误说明)
	 * @param int $error_code 错误码
	 * @param array $param 参数数组 <br />
	 * ················· [可空](str)	$param['default_str']	说明：模式的错误说明。 ['']默认	<br />
	 * ················· [可空/多选_A/必须](unknown_type)	$param['AAAAA']	说明：AAAAA	<br />
	 * @return string 错误说明
	 */
	public static function getUserText( $error_code, array $param = [] ):string {
		// ...
		$param['default_str'] = $param['default_str'] ?? '';
		//
		$code_text = com\Cla\Err\UserText::getText( $error_code );
		if( $code_text == '' ){
			$code_text = self::getText( $error_code, [
				'default_str' => $param['default_str'],
			] );
		}
		//
		return $code_text;
	}

	/**
	 * 返回标准错误码
	 * @param int $status 需要处理的返回值
	 * @param int $default_status 默认返回状态值 [默认:0]
	 * @return int 过滤或的返回值
	 */
	public static function returnMate( $status, $default_status = 0 ){
		if( is_numeric( $status ) && $status <= self::$errorLimitNum){
			com\Lib\Err\GetBack::recordCourse( [ 'type' => 'code->returnMate', 'code' => $status ] );
			return $status;
		}
		//
		com\Lib\Err\GetBack::recordCourse( [ 'type' => 'code->returnMate', 'code' => $default_status ] );
		return com\Lib\Err\GetBack::return( $default_status );;
	}
}
